function doLibrary(library) {
	var items = library.items;
	for (var i = 0; i < items.length; i++) {
		if (items[i].itemType == "movie clip") {
			doMovieClipSymbol(items[i]);
		}
	}
}

function doMovieClipSymbol(symbol) {
	doTimeline(symbol.timeline);
}

function doTimelines(timelines) {
	for (var i = 0; i < timelines.length; i++) {
		doTimeline(timelines[i]);
	}
}

function doTimeline(timeline) {
	doLayers(timeline.layers);
}

function doLayers(layers) {
	for (var i = 0; i < layers.length; i++) {
		doLayer(layers[i]);
	}
}

function doLayer(layer) {
	doFrames(layer.frames);
}

function doFrames(frames) {
	for (var i = 0; i < frames.length; i++) {
		if (i == frames[i].startFrame) {
			doKeyFrame(frames[i]);
		}
	}
}

function doKeyFrame(frame) {
	if (frame.labelType == 'name') {
		doLabelFrame(frame, frame.name);
	}
}

function doLabelFrame(frame, label) {
	var regexp = /^\s*this\.currentLabel\s*=.*$/m;
	var code = "this.currentLabel = '" + addslashes(label) + "';";
	if (regexp.test(frame.actionScript)) {
		frame.actionScript = frame.actionScript.replace(regexp, code);
	} else {
		frame.actionScript = code + "\n\n" + frame.actionScript;
	}
}

function addslashes(str) {
	str = str.replace(/\\/g, "\\\\");
	str = str.replace(/'/g, "\\'");
	return str;
}

fl.outputPanel.clear();
var doc = fl.getDocumentDOM();
doTimelines(doc.timelines);
doLibrary(doc.library);
